#import sys
import urllib2
import re
import logging
import utility.logger

import connector
import settings
import getLink

log = logging.getLogger(settings.log_name_applog)


def retrieve_title(url):
    try:
        response = urllib2.urlopen(url)

    except urllib2.HTTPError, e:
        print e
        return "[CANNOT GET TITLE]"

    except urllib2.URLError, e:
        print e
        return "[CANNOT GET TITLE]"

    source = response.read()

    result = re.search('<title>(.*)</title>', source)

    if not result is None:
        return result.group().strip('</title>')
    else:
        return "[CANNOT_GET_TITLE]"


def write_url():
    filepath = connector.init_latest_link_log_filepath()
    urllist = []

    file_object = open(filepath, 'a')

    try:
        while True:
            url = getLink.get_clip_link()
            if not url in urllist:
                print url
                log.info('got url link: ' + url)
                urllist.append(url)
                file_object.write(url + '\n')

    except KeyboardInterrupt:
        print 'url retrieving finished.'
        log.info('url retrieving finished.')

    file_object.close()


if __name__ == '__main__':
    utility.logger.init()

    write_url()